

package cn.shengchao.aop;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.shengchao.util.helper.LoginHelper;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import top.continew.starter.core.validation.CheckUtils;

import java.lang.reflect.Method;

@Aspect
@Component
public class WxLoginHandlerInterceptor {

    @Pointcut("@within(org.springframework.web.bind.annotation.RestController)")
    public void wxLoginControllerHandler() {
    }

    @Before("wxLoginControllerHandler()")
    public void beforeAdvice(JoinPoint joinPoint) {
        // 获取目标方法的签名
        MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
        // 获取目标方法
        Method method = methodSignature.getMethod();
        // 判断方法需要登录
        if (!method.isAnnotationPresent(SaIgnore.class)) {
            //没有绑定用户且不是绑定用户手机接口
            CheckUtils.throwIf(LoginHelper.getUserId() == null && !"bindPhone".equals(method.getName()), "请先绑定手机号");
        }
    }
}
